/**************************************************************************
 * @file battery_left_handle.c
 * @author BryanHe(贺璧) (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2024-11-11
 * 
 * @copyright Copyright (c) 2024
 * 
 *************************************************************************/
#include "battery_left_handle.h"

uint8_t temp = 0;
ertc_time_type time={0};
static BattLeftDataT batLeftDat;

const mo_u32 _10_pw_table[10ul]={
    1ul,/*10^0*/
    10ul,/*10^1*/
    100ul,/*10^2*/
    1000ul,/*10^3*/
    10000ul,/*10^4*/
    100000ul,/*10^5*/
    1000000ul,/*10^6*/
    10000000ul,/*10^7*/
    100000000ul,/*10^8*/
    1000000000ul,/*10^9*/
};

int get_string_by_number(mo_u32 num,mo_u8 *p_str_buf,mo_u8 str_len,mo_u8 dele_high_zeo_flg){
    mo_u32 _10_pw=1;
    mo_u8 tmp,h_flg=0;
    int i;

    if (!str_len || p_str_buf==NULL){
        return -1;
    }

    _10_pw = _10_pw_table[str_len-1];
    
    for(i=0;i<str_len;i++){
       tmp = num/_10_pw%10;

       if(tmp || h_flg || _10_pw==1 || !dele_high_zeo_flg){
            h_flg = 1;
            p_str_buf[i] = tmp+'0';
       }else{
            p_str_buf[i] = ' ';
       } 
        _10_pw/=10;
    }

    return 0;
}
static void usr_bat_left_ui_update_handle(BattLeftDataT *batLeftDat) {
    u8 s, m, h;
    mo_u8 *p_curr_uint, *p_pw_uint;
    mo_u8 str_buf[16];
    BatLeftDisInfDatT *str;
    mo_u32 tmppp, tt;
    mo_u32 batLeftCurr, batLeftPower, blc_update_s, blp_update_s;

    s = batLeftDat->time % 60;
    m = (batLeftDat->time / 60) % 60;
    h = batLeftDat->time / 3600ul;

    /********************** uA,mA,A /H ****************************/
    tmppp = batLeftDat->curr_uA / 1000ul;
    if (4200000ul < tmppp) {
        batLeftDat->curr_mA += tmppp;
        batLeftDat->curr_uA -= tmppp * 1000ul;
    }
    tmppp = batLeftDat->curr_mA / 1000ul;
    if (4200000ul < tmppp) {
        batLeftDat->curr_A += tmppp;
        batLeftDat->curr_mA -= tmppp * 1000ul;
    }
    if (batLeftDat->curr_A) {
        tmppp = (batLeftDat->curr_A + batLeftDat->curr_mA / 1000ul) / 3600ul;
        p_curr_uint = " AH";

    } else if (batLeftDat->curr_mA) {
        tt = (batLeftDat->curr_mA + batLeftDat->curr_uA / 1000ul);
        tmppp = tt / 3600ul;

        if (tmppp < 10000ul) {
            p_curr_uint = "mAH";
        } else {
            tmppp = tt / 1000ul / 3600ul;
            p_curr_uint = " AH";
        }
    } else {
        tmppp = batLeftDat->curr_uA / 3600ul;
        if (tmppp < 10000ul) {
            p_curr_uint = "uAH";
        } else {
            tmppp = batLeftDat->curr_uA / 1000ul / 3600ul;
            p_curr_uint = "mAH";
        }
    }
    batLeftCurr = tmppp;

    /*获得底部刷新状态信息*/
    tmppp = batLeftDat->curr_uA / 3600ul;
    blc_update_s = tmppp % 10000000ul;
    /************************************************************************************/

    /********************** nW,uW,mW,W /H ***********************************************/
    tmppp = batLeftDat->power_uW / 1000ul;
    if (4200000ul < tmppp) {
        batLeftDat->power_mW += tmppp;
        batLeftDat->power_uW -= tmppp * 1000ul;
    }
    tmppp = batLeftDat->power_mW / 1000ul;
    if (4200000ul < tmppp) {
        batLeftDat->power_W += tmppp;
        batLeftDat->power_mW -= tmppp * 1000ul;
    }
    if (batLeftDat->power_W) {
        tmppp = (batLeftDat->power_W + batLeftDat->power_mW / 1000ul) / 3600ul;
        p_pw_uint = " WH";
    } else if (batLeftDat->power_mW) {
        tt = (batLeftDat->power_mW + batLeftDat->power_uW / 1000ul);
        tmppp = tt / 3600ul;

        if (tmppp < 10000ul) {
            p_pw_uint = "mWH";
        } else {
            tmppp = tt / 1000ul / 3600ul;
            p_pw_uint = " WH";
        }
    } else {
        tt = (batLeftDat->power_uW);
        tmppp = tt / 3600ul;
        if (tmppp < 10000ul) {
            p_pw_uint = "uWH";
        } else {
            tmppp = tt / 1000ul / 3600ul;
            p_pw_uint = "mWH";
        }
    }
    batLeftPower = tmppp;
    tmppp = batLeftDat->power_uW / 3600ul;
    blp_update_s = tmppp % 10000000ul;
    /***************************************************************************************** */

    str = (BatLeftDisInfDatT *)mo_malloc(sizeof(BatLeftDisInfDatT));
    if (str) {
        memset(str, 0, sizeof(*str));
        get_string_by_number(h, str->time_inf_str, 2, 0);
        strcat(&str->time_inf_str[2], ":");
        get_string_by_number(m, &str->time_inf_str[3], 2, 0);
        strcat(&str->time_inf_str[5], ":");
        get_string_by_number(s, &str->time_inf_str[6], 2, 0);

        get_string_by_number(batLeftPower, str->power_inf_str, 4, 1);
        strcat(str->power_inf_str, p_pw_uint);

        get_string_by_number(batLeftCurr, str->curr_inf_str, 4, 1);
        strcat(str->curr_inf_str, p_curr_uint);

        get_string_by_number(blp_update_s, str->power_inf_str_low, 7, 1);
        get_string_by_number(blc_update_s, str->curr_inf_str_low, 7, 1);

        mo_msg_send(get_usr_display_task(), USR_BAT_LEFT_INFO_UPDATE, str, 0);
    }
}

void main_app_bat_left_infor_sync_and_update_ui(void){
    usr_bat_left_ui_update_handle(&batLeftDat);
}

void clear_battLeftRecordInfor(void){
    // BatLeftDisInfDatT *str;
    // str = (BatLeftDisInfDatT*)mo_malloc(sizeof(BatLeftDisInfDatT));
    // memset(str,0,sizeof(*str));
    memset(&batLeftDat,0,sizeof(batLeftDat));
    // if(str){
    //     strcat(str->curr_inf_str,"  00uAH");
    //     strcat(str->power_inf_str,"  00uWH");
    //     strcat(str->time_inf_str,"00:00:00");
    //     strcat(str->curr_inf_str_low,"0000000");
    //     strcat(str->power_inf_str_low,"0000000");
    //     mo_msg_send(get_usr_display_task(),USR_BAT_LEFT_INFO_UPDATE,str,0);  
    // }
    usr_bat_left_ui_update_handle(&batLeftDat);
    
}
/**************************************************************************
 * @brief 电量统计处理
 * 
 * @param out_vol 
 * @param out_curr 
 *************************************************************************/
// void usr_BatteryLeft_handle(mo_u32 out_vol,mo_u32 out_curr,OUT_CTRL_T out_state){
//     // u8 s,m,h;
//     // mo_u8 *p_curr_uint,*p_pw_uint;
//     // mo_u8 str_buf[16];
//     // BatLeftDisInfDatT *str;
//     // mo_u32 tmppp,tt;
//     // mo_u32 batLeftCurr,batLeftPower,blc_update_s,blp_update_s;

//     if(out_state){
//         batLeftDat.time++;
//         batLeftDat.curr_uA+=out_curr;
//         batLeftDat.power_uW += (out_vol/10)*(out_curr/100); /*unit 10^-9W(nW)*/

//         usr_bat_left_ui_update_handle(&batLeftDat);    
//     }

// }
/**************************************************************************
 * @brief 电量统计处理
 * 
 * @param out_vol 
 * @param out_curr 
 * @param out_state  should be OUT_CTRL_DISABLE,OUT_CTRL_DISABLE
 *************************************************************************/
void usr_BatteryLeft_handle(mo_u32 out_vol/*mV*/,mo_u32 out_curr,/*uA*/OUT_CTRL_T out_state){
        /* get the current time */
    ertc_calendar_get(&time);

    if(temp != time.sec){
        temp = time.sec;
        // usr_BatteryLeft_handle(out_vol,out_curr,out_state);
        if(out_state){

            batLeftDat.time++;
            batLeftDat.curr_uA+=out_curr;
            batLeftDat.power_uW += (out_vol/10)*(out_curr/100); /*unit 10^-9W(nW)*/
            usr_bat_left_ui_update_handle(&batLeftDat);    
            
        }
        
    }
}